package com.cksecurity.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.CountDownLatch;

import org.apache.http.Consts;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.cksecurity.bean.ExecuteResult;

public class HttpClient {
    
    private CountDownLatch latch;
    private ExecuteResult execResult;
    
    public HttpClient(CountDownLatch latch, ExecuteResult execResult) {
        super();
        this.latch = latch;
        this.execResult = execResult;
    }

    public void doGet(String url) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        CloseableHttpResponse resp = null;
        try {
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000)
                    .setConnectionRequestTimeout(5000).setSocketTimeout(10000).build();
            //get.setConfig(requestConfig);
            resp = client.execute(get);
            System.out.println("响应码：" + resp.getStatusLine().getStatusCode());
            InputStream is = resp.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
            String str = null;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }
            br.close();
            is.close();
            client.close();
            execResult.getSuccess().incrementAndGet();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            execResult.getFail().incrementAndGet();
        } finally {
            latch.countDown();
        }
    }

    public static void main(String[] args) {
        
    }

}
